package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

/**
 * @author Gyanendra
 *
 */
public class User {
	private int userId;
	private static User user = null;
	private static Logger theLogger = Logger.getLogger("WebUser");

	String query = "";
	
	public static void main(String[] args) {
		User user = getInstance();
		
		DBAdapter adapter = DBAdapter.getInstance();
		adapter.open();
		String query = "DELETE FROM year_details";
		adapter.update(query);
		try {
			int year = 2007;
			for (int i = 0; i < 14; i++) {
				
				query = "INSERT INTO year_details (year) VALUES ('" + year + "')";
				adapter.update(query);
				year ++;
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		adapter.close();
		//user.saveUser(-1, 2, "xc", "cv", "bv", "fg", "fg", "5676878", "dfg@fgh.com", 1, 2);
		
	}

	public User() {
	}

	public static synchronized User getInstance() {
		if(user == null)
			user = new User();
		return user;
	} 
	
	/**
	 * Method use to delete the user.
	 * @param userId
	 */
	public void deleteUser(int userId) {
		DBAdapter adapter = DBAdapter.getInstance();
		adapter.open();
		query = "DELETE FROM user WHERE user_id='" + userId + "'  AND user_id<>1";
		adapter.update(query);
		adapter.close();
	}
	
	/**
	 * Method used to save the user related information in the user table.
	 * @param userId
	 * @param sessionUserId
	 * @param firstName
	 * @param lastName
	 * @param userName
	 * @param userPassword
	 * @param userLevel
	 * @param enabled
	 * @param phoneMobile
	 * @param emailAddress
	 * @param description
	 */
	public String saveUser(int sessionUserId, int userId, String firstName, String lastName, String userName, String userPassword, int userLevel, int enabled, String phoneMobile, String emailAddress, String description) {
		DBAdapter adapter = DBAdapter.getInstance();
		adapter.open();
		ResultSet rs = null;
		String userExit = "-1";
		
		try {
			String query = "Select * from user where user_id != " + userId + " AND user_name='" +userName + "'";
			rs = adapter.select(query);
			if(rs.next()) {
				userExit = "1";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if(userExit.equals("-1")) {
			if(userId == -1) {
				query = "INSERT INTO user (date_added, date_updated) VALUES (now(), now())";
				adapter.update(query);
				query = "SELECT MAX(user_id) from user";
				rs = adapter.select(query);
	
				try {
					while(rs.next()){
						userId  = rs.getInt("Max(user_id)") ;
					}
				} catch(Exception e) {
				}	
			}
			
			query = "UPDATE user " 
				+ " set first_name = \"" + firstName + "\" "
				+ " , last_name = \"" + lastName + "\" "
				+ " , user_name = \"" + userName + "\" "
				+ " , user_password = \"" + userPassword + "\" "
				+ " , user_level = " + userLevel 
				+ " , enabled =" + enabled 
				+ " , phone_mobile = \"" + phoneMobile + "\" "	
				+ " , email_address = '" + emailAddress + "' "	
				+ " , description = \"" + description + "\" "
				+ " , date_added = now() "
				+ " , date_updated = now() "
				+ " , last_update_user_id = '"+ sessionUserId+"' "
				+ " WHERE user_id = " + userId + "";
	//		System.out.println("save user query: " + query);
			adapter.update(query);
		}
		adapter.close();
		return userExit;
	}
}